package com.lagou.servlet;

import com.lagou.model.Student;
import com.lagou.service.UserService;
import com.lagou.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@WebServlet(name = "updateStudentServlet",urlPatterns = "/updateStudentServlet")
public class updateStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 拿到name
        String name = request.getParameter("userName");
        // 拿到id
        Integer id = Integer.parseInt(request.getParameter("code"));
        // 拿到性别
        String sex = request.getParameter("sex");
        // 拿到出生日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = null;
        try {
            birthday = sdf.parse(request.getParameter("birthday"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 拿到邮箱
        String email = request.getParameter("email");
        // 拿到备注
        String remark = request.getParameter("remark");
        Student student = new Student(id,name,sex,birthday,email,remark);
        UserService su = new UserServiceImpl();
        boolean b = su.updateStudent(student);
        if (b==true){
            request.getRequestDispatcher("/showAllUserServlet").forward(request,response);
        }else{
            request.setAttribute("error","错误");
            request.getRequestDispatcher("addStudent.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
